package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.saas.domain.dao.PaymentTaskUserDao;
import com.xbongbong.saas.domain.entity.PaymentTaskUserEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.model.PaymentTaskUserModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: 汪腾伟
 * @date: 2020/8/7
 * @desrcption: 回款计划用户接口实现层
 * @version:1.0
 */
@Service("paymentTaskUserModel")
public class PaymentTaskUserModelImpl implements PaymentTaskUserModel {

    @Resource
    private PaymentTaskUserDao paymentTaskUserDao;

    @Resource
    private PaasEsModel paasEsModel;

    @Resource
    private TransferSubFormHelper transferSubFormHelper;

    @Override
    public Integer insert(UserTeamEntity entity) throws XbbException {
        return null;
    }

    @Override
    public Integer update(UserTeamEntity entity) throws XbbException {
        return null;
    }

    @Override
    public List findEntitys(Map<String, Object> param) {
        return paymentTaskUserDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return null;
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    /**
     * 查询出用户信息
     *
     * @param planIdList
     * @return
     */
    @Override
    public List<PaymentTaskUserEntity> getUserList(List<Long> planIdList) {
        List<PaymentTaskUserEntity> userEntities = paymentTaskUserDao.findUserList(planIdList);
        return userEntities;
    }

    @Override
    public void insertBatch(List<PaymentTaskUserEntity> paymentTaskUserEntities) throws XbbException{
        paymentTaskUserDao.insertBatch(paymentTaskUserEntities);
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_TEAM, paymentTaskUserEntities);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            insertDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_TEAM));
        }
        paasEsModel.insertBatch(insertDTOList);
    }

    /**
     * 批量插入回款计划用户，用于资金三期
     *
     * @param paymentTaskUserEntityList
     * @return
     */
    @Override
    public void insertBatchIncludId(List<PaymentTaskUserEntity> paymentTaskUserEntityList) throws XbbException {
        paymentTaskUserDao.insertBatch(paymentTaskUserEntityList);
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_TEAM, paymentTaskUserEntityList);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            insertDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_TEAM));
        }
        paasEsModel.insertBatch(insertDTOList);
    }

    /**
     * 无差别更新回款计划执行者表
     *
     * @param paymentTaskUserUpdateEntities
     * @return
     */
    @Override
    public void updateAll(List<PaymentTaskUserEntity> paymentTaskUserUpdateEntities) throws XbbException {
        paymentTaskUserDao.updateAll(paymentTaskUserUpdateEntities);
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_TEAM, paymentTaskUserUpdateEntities);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            insertDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_TEAM));
        }
        paasEsModel.updateBatch(insertDTOList);
    }

    /**
     * 批量删除回款计划执行者表中的信息
     *
     * @param dataIdList 回款计划执行者表中得id
     * @param corpid 公司id
     * @return
     * @throws XbbException
     */
    @Override
    public Integer deleteBatch(List<Long> dataIdList, String corpid) throws XbbException {
        int num = paymentTaskUserDao.deleteBatch(dataIdList, corpid);
        return num;
    }


}
